/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package definitions;

import org.jsoup.select.Elements;
import org.pircbotx.hooks.*;
import org.pircbotx.hooks.events.*;
import org.pircbotx.hooks.types.*;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.util.Date;
/**
 *
 * @author tangd
 */
public class DefinitionFetcher{
    
    private LinkedHashSet<String> enabledChannels = new LinkedHashSet<String>();
    protected PreparedStatement getStatement = null;
    protected PreparedStatement setStatement = null;
    private PreparedStatement removeStatement = null;
    private Connection connection = null;
    String table = null;
    public DefinitionFetcher(Connection c, String table) {
        this.table= table;
        this.connection = c;
        try {
            getStatement = c.prepareStatement("SELECT * FROM "+table+" WHERE `word` LIKE ?");
            setStatement = c.prepareStatement("INSERT INTO "+table+" VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE `definition`=?, `definer`=?, `time`=?");
            removeStatement = c.prepareStatement("DELETE FROM "+table+" WHERE `word`=? LIMIT 1");
        }
        catch(SQLException sqlEx) {
            getStatement = null;
            setStatement = null;
        }
    }
    
 
   
    public Definition getDefinition(String word) {
        Definition def = null;
            try {
                getStatement.setString(1, word);
                ResultSet results = getStatement.executeQuery();
                System.out.println("query");
                if(results.next()) {
                //results.getString("word");
                    String definition = results.getString("definition");
                    String definer = results.getString("definer");
                    long time = results.getLong("time");
                    String w = results.getString("word");
                    def = new Definition(w, definition, definer, time);
                    return def;
                }
                return new Definition(word, null, null, (new Date()).getTime());
            }
            catch(SQLException sqlEx) {
                return new Definition(word, null, null, (new Date()).getTime());
            }
    }
    public Definition setDefinition(String word, String definition, String definer) {
        if(true) {
            //"INSERT INTO "+table+" VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE `definition`=?, `definer`=?, `time`=?"
            try {
                long currentTime = (new Date()).getTime();
                setStatement.setString(1, word);
                setStatement.setString(2, definition);
                setStatement.setString(3, definer);
                setStatement.setLong(4, currentTime);
                setStatement.setString(5, definition);
                setStatement.setString(6, definer);
                setStatement.setLong(7, currentTime);
                setStatement.execute();
                Definition d = new Definition(word, definition, definer, currentTime);
                return d;
            }
            catch(SQLException sqlEx) {
                System.out.println("Derp "+sqlEx.getSQLState());
                System.out.println(sqlEx.getMessage());
                return null;
            }
        }
        return null;
    }
    
    public int removeDefinition(String word) {
         try {
             removeStatement.setString(1, word);
             int ret = removeStatement.executeUpdate();;
             return ret;
         }
         catch(SQLException sqlEx) {
             return -1;
         }
    }
}
